package com.uchengwang.ui.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.View;

import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnHttpResultSingleListener;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.base_ui.bean.MoreBean;
import com.uchengwang.ui.cm_ui.cycle.bean.H5ReportBean;
import com.uchengwang.ui.cm_ui.cycle.bean.H5ShareBean;
import com.uchengwang.ui.cm_ui.cycle.bean.SetupMenusBean;
import com.uchengwang.ui.cm_ui.cycle.http.CycleHttpUtils;
import com.uchengwang.ui.cm_ui.ucity.ui.ReportActivity;
import com.uchengwang.ui.listener.PermissionsListener;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;


/**
 * Created by U城网 on 2017/8/22.
 */

public class MoreOperateUtils {

    private static MoreOperateUtils utils;
    private MoreOperateUtils (){}

    public static MoreOperateUtils getUtils() {
        if (utils == null){
            utils = new MoreOperateUtils();
        }
        return utils;
    }

    /**
     * 社圈操作
     * */
    public void more(final Activity activity, final View bottomView, final SetupMenusBean bean){

        if (bean == null){
            UCommonUtil.getUtil().showToast(activity, "正在加载信息");
            return;
        }
        final List<MoreBean> beanList = new ArrayList<MoreBean>();
        if (bean.getShare() != null){
            beanList.add(new MoreBean(R.drawable.selector_share_wx, "微信朋友"));
            beanList.add(new MoreBean(R.drawable.selector_share_friend, "微信朋友圈"));
        }
        if (bean.getReport() != null ){
            if (bean.getReport().getReportUid() ==UCommonUtil.getUtil().getId()){
                //自己发布的信息  删除
                beanList.add(new MoreBean(R.drawable.selector_report, "删除"));
            }else {
                beanList.add(new MoreBean(R.drawable.selector_report, "举报"));
            }
        }

        if (bean.getBlockId() >0){
//            beanList.add(new MoreBean(R.drawable.selector_report, "屏蔽"));
        }


        PopupUtils.getUtils().bottomMore(activity, bottomView, beanList, new PopupUtils.OnItemPopupClickListener() {
            @Override
            public void onItemClick(int index, final String title) {
                PopupUtils.getUtils().dismiss();

                if (UCommonUtil.getUtil().isNeedLogin(activity, false)){
                    return;
                }
                switch (title){
                    case "发送给朋友":
//                        RongChatManager.getUtils().createMsg(bean, activity);
                        break;

                    case "撤回":
                        break;

                    case "删除":
//                        HttpCityPresenter.getPresenter().withdrawDelete(activity, bean.getId(), null);
                        CycleHttpUtils.getUtils().deleteCycleInfo(activity, bean.getReport().getReferId(), bean.getReport().getReferType(), new OnResultListener() {
                            @Override
                            public void onResult(int stutas, String msg, int total, String data) {
                                if (stutas ==0){
                                    UCommonUtil.getUtil().showToast(activity, "删除成功");
                                    EventBus.getDefault().post(BaseConfig.WEB_SHARE_DELETE_SUCCESS);//用户刷新网页 (也可能用不到)

                                    H5ReportBean h5ReportBean = new H5ReportBean();
                                    h5ReportBean.setReferType(bean.getReport().getReferType());
                                    h5ReportBean.setReferId(bean.getReport().getReferId());
                                    EventBus.getDefault().post(h5ReportBean);

                                }else {
                                    UCommonUtil.getUtil().showToast(activity, "删除成功");
                                }
                            }
                        });
                        break;

                    case "举报":
//                        Intent intent = new Intent(activity, ReportActivity.class);
//                        intent.putExtra(BaseConfig.ID, bean.getId());
//                        intent.putExtra(BaseConfig.TYPE, bean.getPubType());
//                        activity.startActivity(intent);
                        if (bean.getReport().getUid() >0){
                            //说明是举报人
                            Intent in = new Intent(activity, ReportActivity.class);
                            in.putExtra(BaseConfig.TYPE, BaseConfig.REPORT_PERSON_TYPE);
                            in.putExtra(BaseConfig.ID, bean.getReport().getUid());
                            activity.startActivity(in);
                        }else {
                            //说明举报信息
                            Intent intent = new Intent(activity, ReportActivity.class);
                            intent.putExtra(BaseConfig.TYPE, BaseConfig.REPORT_CYCLE_TYPE);
                            intent.putExtra(BaseConfig.BEAN, bean.getReport());
                            activity.startActivity(intent);
                        }

                        break;
                    case "微信朋友"://分享给微信朋友
                    case "微信朋友圈"://分享给温馨朋友圈
                        final Bitmap bitmap = BitmapFactory.decodeResource(activity.getResources(), R.drawable.weixin_share);
                        final H5ShareBean shareBean = bean.getShare();
                        if (!TextUtils.isEmpty(shareBean.getCover())){
                            //先检查有没有权限
                            PermissionUtils.getUtils().checkStorage(activity, new PermissionsListener() {
                                @Override
                                public void onSuccess() {
                                    LoadingUtils.getUtils().loading(activity);
                                    ImageManager.getInstance().getBitmap(activity, shareBean.getCover(), new SimpleTarget<Bitmap>() {
                                        @Override
                                        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                            if (resource == null){
                                                shareToWx(activity, title, shareBean.getUrl(), shareBean.getTitle(), shareBean.getDesc(), bitmap);

                                            }else {
                                                ImageManagerUtils.getUtils().wxShareBitmap(activity, resource, PathUtils.UCW_COM_IMAGE, "wx_" + System.currentTimeMillis() + shareBean.getCover().substring(shareBean.getCover().lastIndexOf(".")), new OnHttpResultSingleListener() {
                                                    @Override
                                                    public void onResult(int status, String msg, Object o) {
                                                        LoadingUtils.getUtils().hide();
                                                        if (o != null){
                                                            shareToWx(activity, title, shareBean.getUrl(), shareBean.getTitle(), shareBean.getDesc(), (Bitmap) o);
                                                        }else {
                                                            shareToWx(activity, title, shareBean.getUrl(), shareBean.getTitle(), shareBean.getDesc(), bitmap);
                                                        }
                                                    }
                                                });
                                            }

                                        }
                                    });
                                }

                                @Override
                                public void onFailed() {
                                    UCommonUtil.getUtil().showToast(activity, "请授权");
                                }
                            });

                        }else {
                            shareToWx(activity, title, shareBean.getUrl(), shareBean.getTitle(), shareBean.getDesc(), bitmap);
                        }
                        break;

                    case "屏蔽":
                        CycleHttpUtils.getUtils().blockShare(activity, bean.getBlockId(), new OnResultListener() {
                            @Override
                            public void onResult(int stutas, String msg, int total, String data) {
                                if (stutas ==0){
                                    UCommonUtil.getUtil().showToast(activity, "屏蔽成功");
                                }else {
                                    UCommonUtil.getUtil().showToast(activity, "屏蔽失败");
                                }
                            }
                        });

                        break;
                }
            }
        });

    }


    /**
     * @param title 微信朋友圈 还是微信朋友
     * @param pubTitle 标题
     * @param desc 描述
     * @param bitmap bitmap图片
     * @param url 分享地址
     * */
    private void shareToWx(Context context, String title, String url, String pubTitle, String desc, Bitmap bitmap){
        if (title.equals("微信朋友")){
            ShareToWxUtils.getUtils(context).shareUrl(url, pubTitle, desc, bitmap, SendMessageToWX.Req.WXSceneSession);
        }else {
            ShareToWxUtils.getUtils(context).shareUrl(url, pubTitle+": "+desc, pubTitle+": "+desc, bitmap, SendMessageToWX.Req.WXSceneTimeline);
        }
    }

}
